<template>
  <div class="aui-card-list">
    <uni-list-query-card :queryShow="queryShow" @query-click="getDataList()" @click="(e)=>{this.queryShow=e}" @searchReset="searchReset()">
      <el-col :span="6">
        <el-form-item :label="$t('role.name')">
          <el-input v-model="dataForm.name" :placeholder="$t('role.name')" clearable></el-input>
        </el-form-item>
      </el-col>
    </uni-list-query-card>
    <el-card shadow="never" class="aui-card-list-body">
      <vxe-toolbar :setting="{immediate:true}">
        <template v-slot:buttons>
          <el-button v-if="$hasPermission('sys:role:save')" type="primary" @click="addOrUpdateHandle()">
            {{ $t('add') }}
          </el-button>
          <el-button v-if="$hasPermission('sys:role:delete')" type="danger" @click="deleteHandle()">
            {{ $t('deleteBatch') }}
          </el-button>
        </template>
        <template v-slot:tools>
          <el-button icon="el-icon-refresh" @click="getDataList()" circle></el-button>
        </template>
      </vxe-toolbar>
      <div class="aui-card-list-body-table">
        <vxe-table :loading="dataListLoading" :data="dataList" ref="xTable" auto-resize class="vxe-table-element" @select-change="dataListSelectionChangeHandleVex" @select-all="dataListSelectionAllChangeHandleVex" max-height="auto">
          <vxe-table-column type="checkbox" width="50" align="center"></vxe-table-column>
          <vxe-table-column field="name" title="role.name"></vxe-table-column>
          <vxe-table-column field="deptName" title="role.deptName" width="140" align="center"></vxe-table-column>
          <vxe-table-column field="roleGradeName" title="role.roleGradeName" width="120" align="center">
            <template slot-scope="scope">
              <el-tag style="width:70px;" v-if="scope.row.roleGrade === '1'" size="small" type="warning">
                {{ scope.row.roleGradeName }}
              </el-tag>
              <el-tag style="width:70px;" v-else-if="scope.row.roleGrade === '2'" size="small" type="success">
                {{ scope.row.roleGradeName }}
              </el-tag>
              <el-tag style="width:70px;" v-else size="small">{{ scope.row.roleGradeName }}</el-tag>
            </template>
          </vxe-table-column>
          <vxe-table-column field="remark" title="role.remark"></vxe-table-column>
          <vxe-table-column field="createDate" title="role.createDate" sortable width="140" align="center">
          </vxe-table-column>
          <vxe-table-column title="handle" width="180" align="center">
            <template slot-scope="scope" align="center">
              <el-button v-if="$hasPermission('sys:role:update')" type="text" icon="el-icon-edit" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}
              </el-button>
              <el-button v-if="$hasPermission('sys:role:update')" type="text" icon="el-icon-user" size="small" @click="roleUserHandle(scope.row)">{{ $t('role.user') }}
              </el-button>
              <el-button v-if="$hasPermission('sys:role:delete')" icon="el-icon-delete" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}
              </el-button>
            </template>
          </vxe-table-column>
        </vxe-table>
        <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      </div>
    </el-card>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './role-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/sys/role/page',
        getDataListIsPage: true,
        deleteURL: '/sys/role',
        deleteIsBatch: true
      },
      dataForm: {
        name: ''
      }
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    roleUserHandle (row) {
      var router = {
        name: 'sys-role-user',
        id: row.id,
        roleId: this.$route.meta.roleId,
        oldName: 'sys-role',
        path: 'sys/role-user',
        title: row.name + '角色的用户'
      }

      var params = {
        roleIds: row.id,
        roleGrade: row.roleGrade
      }
      this.$options.methods.openNewTab(this.$router, router, params)
    }
  }
}
</script>
